import axios from 'axios'
const baseurl = "http://www.chatgod.cn:8082/"
//const baseurl = "http://127.0.0.1:8082/"
const baseapiurl = baseurl+"api/"
const baseweburl = "http://www.chatgod.cn/"
// 创建 Axios 实例
const instance = axios.create({
  baseURL: baseapiurl, // 接口根路径
  timeout: 10000, // 请求超时时间
})


instance.interceptors.response.use(
  response => {
    if (response.status == 250) {
      let user = uni.getStorageSync('user');
      if(user)
      {
        user.token = ''
        uni.setStorageSync('user',user)
      }
      uni.redirectTo({ url: '/pages/login/login' })
      return response
    }
    else {
      return response
    }

  },
  error => {
    if (error && error.response && error.response.status === 401) {
      // token 失效，跳转到登录页面
      console.log(error)
      uni.redirectTo({ url: '/pages/login/login' })
    } else {
      return Promise.reject(error)
    }
  }
)


function registerApi(apiMethod, apiUrl) {
  return function (data) {
    return instance.request({
      url: baseapiurl + apiUrl,
      method: apiMethod,
      data: data
    })
  }
}

function registerParamApi(apiMethod, apiUrl) {
  return function (data) {
    return instance.request({
      url: baseapiurl + apiUrl,
      method: apiMethod,
      params: data
    })
  }
}

function setToken(token) {
  instance.defaults.headers.common['token'] = token
}

//得到整个集合
function getCollectWithMd5(funMd5,funGet,name) {
  //得到本地客户集合md5
  let collect = uni.getStorageSync(name);
  let md5 = "none"
  if(collect==null)collect = {data:[],md5:md5}
  return funMd5().then(response => {
    if (response.status == 200) {
      md5 = response.data.md5
      if (collect.md5 == md5) {
        console.log(name+" not need download")
        response.data.data = collect.data
        return response
      }else 
      {
        return funGet({ limit: 1000 })
      }
    }else
    return response
  }).then(response => {
    if (response.status == 200) {
      collect = {data:response.data.data,md5:md5}
      uni.setStorageSync(name,collect);
    }
    return response
  })
}




export default {
  baseUrl:baseurl,
  baseApiUrl:baseapiurl,
  baseWebUrl:baseweburl,
  //客户
  createCustomer: registerApi('post', 'Customer'),
  getCustomers: registerParamApi('get', 'Customer'),
  delCustomers: registerApi('delete', 'Customer'),
  updaCustomers: registerApi('put', 'Customer'),
  md5Customers: registerApi('post', 'CustomerMd5'),
  //物料
  createMaterial: registerApi('post', 'Material'),
  getMaterials: registerParamApi('get', 'Material'),
  delMaterials: registerApi('delete', 'Material'),
  updaMaterials: registerApi('put', 'Material'),
  md5Materials: registerApi('post', 'MaterialMd5'),
  //用户
  createUser: registerApi('post', 'User'),
  getUsers: registerParamApi('get', 'User'),
  delUsers: registerApi('delete', 'User'),
  updaUsers: registerApi('put', 'User'),
  md5Users: registerApi('post', 'UserMd5'),
  //工单
  createWorkOrder: registerApi('post', 'WorkOrder'),
  getWorkOrders: registerParamApi('get', 'WorkOrder'),
  delWorkOrders: registerApi('delete', 'WorkOrder'),
  updaWorkOrders: registerApi('put', 'WorkOrder'),
  //glecard
  createGelCard: registerApi('post', 'GelCard'),
  getGelCards: registerParamApi('get', 'GelCard'),
  delGelCards: registerApi('delete', 'GelCard'),
  updaGelCards: registerApi('put', 'GelCard'),
  //角色
  createRole: registerApi('post', 'Role'),
  getRoles: registerParamApi('get', 'Role'),
  delRoles: registerApi('delete', 'Role'),
  updaRoles: registerApi('put', 'Role'),
  //接单
  acceptWorkOrders: registerApi('post', 'Accept'),
  //登陆
  login: registerApi('post', 'Login'),
  getHomepageStatistics: registerApi('post', 'HomepageStatistics'),
  getMinepageStatistics: registerApi('post', 'MinepageStatistics'),
  //生成GenExcel
  genExcel: registerApi('post', 'GenExcel'),
  //发送群消息
  sendGroupMessages: registerApi('post', 'SendGroupMessages'),
  //得到整个集合
  getCollectWithMd5:getCollectWithMd5,
  setToken: setToken,
}